package com.代码随想录;

import java.util.HashSet;

/**
 * @author zsj
 * @version 1.0
 * @date 2024/5/13 15:28
 * @description
 */
public class HashTableDemo {

    public static void main(String[] args) {
        HashTableDemo demo = new HashTableDemo();
        System.out.println(demo.isAnagram("anagram", "nagaram"));
    }

    //242. 有效的字母异位词
    public boolean isAnagram(String s, String t) {
        int[] arr = new int[26];
        char[] as = s.toCharArray();
        char[] bs = t.toCharArray();
        for (char a : as) {
            arr[a - 'a']++;
        }
        for (char b : bs) {
            arr[b - 'a']--;
        }
        for (int i : arr) {
            if (i != 0) return false;
        }
        return true;
    }

    //349. 两个数组的交集
    public int[] intersection(int[] nums1, int[] nums2) {
        HashSet<Integer> set = new HashSet<>();
        for (int i : nums1) {
            set.add(i);
        }
        HashSet<Integer> list = new HashSet<>();
        for (int i : nums2) {
            if (set.contains(i)) {
                list.add(i);
            }
        }
        return list.stream().mapToInt(e -> e).toArray();
    }

    //202. 快乐数
    public boolean isHappy(int n) {
        HashSet<Integer> set = new HashSet<>();
        while (n != 1 && !set.contains(n)) {
            set.add(n);
            n = getNumber(n);
        }
        return n == 1;
    }

    private int getNumber(int n) {
        int ans = 0;
        while (n > 0) {
            int temp = n % 10;
            ans += temp * temp;
            n /= 10;
        }
        return ans;
    }

    //454. 四数相加 II
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        return 1;
    }

}
